package cn.shuangliang.contract.web;

import cn.shuangliang.contract.model.Contract;
import cn.shuangliang.contract.model.Contracthistory;
import cn.shuangliang.contract.model.Contractitem;
import cn.shuangliang.contract.model.Product;
import cn.shuangliang.contract.service.ContractService;
import cn.shuangliang.contract.service.ProductService;
import cn.shuangliang.contract.util.LogUtil;
import cn.shuangliang.contract.util.StrUtil;
import cn.shuangliang.contract.vo.Page;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author oldliu
 * @since 1.0
 */
public class ContractController {
    private ContractService contractService=new ContractService();
    private ProductService productService=new ProductService();
    //首页提供合同号，品种，日期范围的四个条件查找
    public String index(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int p=StrUtil.toInt(request.getParameter("pageNo"),1);
        String no= StrUtil.trim(request.getParameter("no"));
        String type= StrUtil.trim(request.getParameter("pname"));
        Date start= StrUtil.toDate(request.getParameter("start"));
        Date end= StrUtil.toDate(request.getParameter("end"));

        request.setAttribute("no",no);
        request.setAttribute("pname",type);
        request.setAttribute("start",start);
        request.setAttribute("end",end);
        Page<Contract> page = contractService.search(p, no, type, start, end);
        request.setAttribute("page",page);

        return "/WEB-INF/views/contracts.html";
    }
    public String toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        Page<Product> page = productService.search(1, Integer.MAX_VALUE, null, null);
        request.setAttribute("page",page);
        return "/WEB-INF/views/addcontract.html";
    }
    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String contractno= StrUtil.trim(request.getParameter("contractno"));
        Date ctime= StrUtil.toDate(request.getParameter("ctime"));
        String billno= StrUtil.trim(request.getParameter("billno"));
        String marker= StrUtil.trim(request.getParameter("marker"));
        String seller= StrUtil.trim(request.getParameter("seller"));
        Contract cr=new Contract(contractno,ctime,billno,marker,seller);

        String[]pids=request.getParameterValues("pid");
        String[]weight=request.getParameterValues("weight");
        String[]num=request.getParameterValues("num");
        List<Contractitem> list=new ArrayList<>();
        for(int i=0;i<pids.length;i++){
            Contractitem ci=new Contractitem();
            ci.setPid(Long.parseLong(pids[i]));
            ci.setWeight(StrUtil.toDouble(weight[i]));
            ci.setNum(StrUtil.toInt(num[i]));
            ci.setNotweight(ci.getWeight());
            ci.setNotatnum(ci.getNum());
            list.add(ci);
        }
        int r = this.contractService.add(cr, list);
        response.sendRedirect("c-index.do?msg="+ URLEncoder.encode(r>0?"增加成功":"增加失败","utf-8"));
    }

    public void confirmOver(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        Long id=StrUtil.toLong(request.getParameter("id"));
        int r=this.contractService.confirm(id,true);
        //response.setContentType("application/json;charset=utf-8");
        //response.getWriter().print("{\"success\":"+(r>0?true:false)+"}");
        response.sendRedirect("c-index.do?msg="+URLEncoder.encode(r>0?"修改成功":"修改失败","utf-8"));
    }
//    public void showhistory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
//        Long id=StrUtil.toLong(request.getParameter("id"));
//
//    }
    public String find(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        Long id=StrUtil.toLong(request.getParameter("id"));
        request.setAttribute("contract",this.contractService.find(id));

        return "/WEB-INF/views/contract.html";
    }
    public String findWithHis(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        Long id=StrUtil.toLong(request.getParameter("id"));
        request.setAttribute("contract",this.contractService.findWithHis(id));

        return "/WEB-INF/views/contract-his.html";
    }
    public String toAddHis(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        Long id=StrUtil.toLong(request.getParameter("id"));
        request.setAttribute("contract",this.contractService.findWithHis(id));
        return "/WEB-INF/views/contract-his.html";
    }
    public void addhis(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        Long id=StrUtil.toLong(request.getParameter("id"));
        Contracthistory c=new Contracthistory();
        c.setCid(id);
        c.setProdid(StrUtil.toLong(request.getParameter("pid")));
        c.setAtweight(StrUtil.toDouble(request.getParameter("atweight")));
        c.setAtnum(StrUtil.toInt(request.getParameter("atnum")));
        c.setAttime(StrUtil.toDate(request.getParameter("attime")));
        c.setMarker(StrUtil.trim(request.getParameter("marker")));

       // LogUtil.info(c+"????????????????????");
        int r=this.contractService.addHis(c);
        //return "/WEB-INF/views/addhis.html";
       // request.getSession().setAttribute("msg",r>0?"增加成功":"增加失败");
        response.sendRedirect("c-toAddHis.do?id="+id+"&msg="+URLEncoder.encode(r>0?"增加成功":"增加失败,请核对数量","utf-8"));
    }

    public String contractToUp(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        Long id=StrUtil.toLong(request.getParameter("id"));
        request.setAttribute("contract",this.contractService.findWithHis(id));
        return "/WEB-INF/views/updatecontract.html";
    }

    public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        Long id=StrUtil.toLong(request.getParameter("id"));

        String contractno= StrUtil.trim(request.getParameter("contractno"));
        Date ctime= StrUtil.toDate(request.getParameter("ctime"));
        String billno= StrUtil.trim(request.getParameter("billno"));
        String marker= StrUtil.trim(request.getParameter("marker"));
        String seller= StrUtil.trim(request.getParameter("seller"));
        Contract cr=new Contract(contractno,ctime,billno,marker,seller);
        cr.setId(id);
        int r=this.contractService.update(cr);

        response.sendRedirect("c-index.do?msg="+URLEncoder.encode(r>0?"修改成功":"修改失败","utf-8"));
    }

    public void delItem(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        Long id=StrUtil.toLong(request.getParameter("id"));
        Long cid=StrUtil.toLong(request.getParameter("cid"));
        int r=this.contractService.delCItem(id,cid);

        response.sendRedirect("c-contractToUp.do?id="+cid+"&msg="+URLEncoder.encode(r>0?"删除成功":"有到货记录，不能删除","utf-8"));
    }
}
